// 蹲饼
const axios = require('axios')
const Oumirai = require('./oumirai')
const middle = require('./middle')
const config = require('../config/conf')

function getAkCookie(firstLink = false) {
    const uid = config.cookieIds || [6279793937]
    for (const item of uid) {
        axios({
            method: 'get',
            url: `https://m.weibo.cn/api/container/getIndex?type=uid&value=${item}&containerid=107603${item}`
        }).then(res => {
            let data = res.data
            if (data.ok == 1 && data.data != null && data.data.cards != null && data.data.cards.length > 0) {
                for (const item of data.data.cards) {
                    let weiboUrl = item.scheme
                    // 过滤转发内容
                    if (item.hasOwnProperty('mblog') && item.mblog.hasOwnProperty('retweeted_status')) {
                        continue
                    }
                    if (item.hasOwnProperty('mblog')) {
                        let mblog = item.mblog
                        // 过滤置顶微博
                        if (item.profile_type_id.indexOf('top') != -1 || (mblog.hasOwnProperty('isTop') && mblog.isTop == 1)) {
                            continue
                        }
                        // 是否第一次连接，将所有的id都存进内存缓存
                        if (firstLink) {
                            middle.cookieIdList[mblog.id] = true
                        }
                        // 获取最新的饼
                        else {
                            if (!middle.cookieIdList[mblog.id]) {
                                newCookie(mblog, weiboUrl)
                            }
                            middle.cookieIdList[mblog.id] = true
                        }



                    }
                }
            }
            else
                console.log('可能是微博炸了，不关我的事')
        })
    }

}

function newCookie(info, url) {
    let om = new Oumirai({
        order: '',
        group: config.cookieGroups,
        msgFormat: '',
        qq: ''
    })
    let content = info.raw_text ||
        info.text
            .replace(
                /<a.*?>|<\/a>|<\/span>|<\span.*>|<span class="surl-text">|<span class='url-icon'>|<span class="url-icon">|<img.*?>|全文|网页链接/g,
                ''
            )
            .replace(/<br \/>/g, '\n')
    let img = ''
    let forwardImg = []
    // 是否是视频微博
    if (info.page_info && info.page_info.type == 'video') {
        img = info.page_info.page_pic.url
    }
    // 是否包含图片
    if (info.pics && info.pics instanceof Array && info.pics.length > 0) {
        for (const item of info.pics) {
            forwardImg.push({
                type: 'Image',
                url: item.large.url
            })
        }
        img = forwardImg[0].url
    }

    // 群发群消息
    om.sendSinlgsMessage(`${content}\n相关链接：${url}`)
    if (img) {
        om.sendGroupsMessage(
            om.sendGroupImg([img])
        )
    }
    // 图片多需要发送合并消息
    if (forwardImg.length > 1) {
        om.sendGroupsMessage(
            om.sendForwardMessage(forwardImg)
        )
    }
}

// 第一次连接
exports.firstLink = () => {
    getAkCookie(true)
    // 轮询
    setInterval(() => {
        console.log('蹲饼中...')
        getAkCookie()
    }, config.cookieTime)
}